【问题标题】:How do I edit a table in shiny before outputting it?如何在输出之前编辑闪亮的表格?
【发布时间】:2021-11-23 14:23:24
【问题描述】:

我正在尝试编写一个闪亮的应用程序来容纳一个特定的函数,该函数从采样数据中估计鱼的数量。该函数创建一个对用户来说是无意义的合并变量。代码确实运行,但我试图在创建对用户有意义的变量之后修改此表。为此,我需要将无意义变量拆分为多个部分,重命名这些部分,并指定要打印的部分。我可以使用 mutate 在 tidyverse 中执行此操作,但还没有弄清楚如何或在何处合并这些更改,以免它杀死应用程序。 我在服务器中尝试过反应式。我试图在 renderTable 中执行这些更改。 在下面的代码中,estimate 是自定义函数 MRIP.catch 的输出,需要修改输出。有一个名为“域”的输出列合并了所有输入。我需要将它们分开,以便用户知道他们在表输出中查看的内容。 我知道这段代码不能单独运行。我只是希望这是一个简单的语法问题,有人可以帮助我解开。我一直无法找到在计算之后但在显示之前需要更改的表格示例。

server <- function(input, output, session) {

      sp<-eventReactive(input$go,{input$species})
      yr1<-eventReactive(input$go, {input$start_yr})
      yr2<-eventReactive(input$go, {input$end_yr})

freq2<-eventReactive(input$go,{
                               case_when(input$freq =='annual'~annual,
                                         input$freq =='wave'~wave) 
                                       
                                       })

sub<-eventReactive(input$go, {
                              case_when(input$reg =='by state'~state,
                                        input$reg =='by coast'~coast)
                                       })
mode<-eventReactive(input$go, {
                               case_when(input$modes=='all modes combined'~all_mode,
                                          input$modes=='all modes by mode'~each_mode) 
                                        })

area<-eventReactive(input$go, {
                               case_when(input$areas == 'all areas combined'~all_area,
                                         input$areas=='all areas by area'~each_area)
                                       })

dom1<- eventReactive(input$go, {list(wave=freq2()#Use for annual estimate. Comment out for wave
           ,sub_reg=sub() #Use for custom geo regions 
           ,mode_fx=mode() #use to combine modes. 
           ,area_x=area() #Use to combine fishing areas. 
           
)})


 estimate<-eventReactive(input$go,{
    MRIP.catch(intdir='C:\\Users\\', 
                      st = 12, styr = yr1(), endyr= yr2(), common = sp()
               , dom = dom1()
               )})
 

   
output$species <- renderText({paste( 'you have seletected',sp()) })
output$range<-renderText({paste ('from',yr1(), 'to', yr2())})
output$table<-renderTable({estimate()})

}

以下是我在 dplyr 中用于创建变量的独立部分并重命名它们的代码。我确信这不是最优雅的方式,但它确实有效。

##Separates out each piece of domain to name 
estimate<-
estimate%>%
  mutate (yr = substr(Domain, 5,8),
      wave1=substr(Domain,13,13),
      basin1=substr(Domain,25,25),
      mode1=substr(Domain, 33,33),
      area1=substr(Domain, 40,40),
      cntys1=substr(Domain, 45,45),
      yr_wave=paste(yr,wave1, sep='-'))

estimate<-
    estimate%>%
      mutate (basin = case_when (basin1 == '6' ~'SA', 
                         basin1=='7'~'Gulf',
                         basin1=='1'~'statewide'
                         ),
           mode = case_when(mode1=='1'~'combined',
                            mode1 =='3'~'Shore',
                            mode1=='5'~'Charter',
                            mode1=='7'~'Private'),
           area = case_when(area1 =='1'~'EC state',
                            area1=='2'~'EC fed',
                            area1=='3'~'Gulf state',
                            area1=='4'~'Gulf fed',
                            area1=='5'~'Inland'))

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    我会尽量集中在这部分:“查找计算后显示前需要更改的表格示例”。

    看看下面的例子,看看这是否对你有帮助。

    library(shiny)
    
    ui <- fluidPage(
      actionButton("go", "Go"),
      tableOutput("table")
    )
    
    server <- function(input, output, session) {
      
      df <- reactiveVal(data.frame(a = c(1, 2))) # but reactiveVal() can be left empty as well, then it starts with NULL value
      
      initial_data <- reactive({
        first_computation <- df() %>% 
          mutate(b = c(3, 4))
        df(first_computation )
      })
      
      observeEvent(input$go, {
        second_computation <- initial_data() %>% 
          mutate(c = c(5, 6))
        df(second_computation)
      })
      
      output$table <- renderTable({
        req(input$go) # not sure if this will be enough for your needs!
        df()
      })
      
    }
    
    shinyApp(ui, server)
    

    我创建了reactiveVal 对象,这是最重要的部分——这个对象可以在不同的地方使用(主动-反应上下文)并且可以修改。首先是带有一个变量的data.frame,然后我进行了一些计算,但不显示任何内容。然后,当用户单击“go”并显示新表时,我进行了一些新的额外计算。

    【讨论】:

      猜你喜欢
      • 2015-03-05
      • 2021-06-20
      • 1970-01-01
      • 1970-01-01
      • 2020-02-23
      • 2019-10-25
      • 1970-01-01
      • 1970-01-01
      • 2019-08-01
      相关资源
      最近更新 更多