r
  • shiny
  • 2016-12-06 40 views 0 likes 
    0

    ui.R:閃亮使用一個字符串變量列名

    library(shiny) 
    shinyUI( fluidPage(fluidRow(
        column(input$PYR, 
         style = 'padding:25px', 
         textInput("N_PYR", NULL, width = '192px'), 
         textInput("RATE_PYR", NULL, width = '192px'), 
         width = 3), 
        column(input$CYR, 
          style = 'padding:25px', 
          textInput("N_CYR", NULL, width = '192px'), 
          textInput("RATE_CYR", NULL, width = '192px'), 
          width = 3) 
    
    ))) 
    

    server.R:

    library(shiny) 
    shinyServer(function(input, output, session) { 
        # current year ("2015-16" for example) 
        input$CYR <- reactive({"2015-16"}) 
    
        input$PYR <- reactive({paste0(
        as.numeric(substr(input$CYR, start = 1, stop = 4)-1), 
        "-", 
        as.numeric(substr(input$CYR, start = 6, stop = 7)-1)) 
        }) 
    
        # terminate when window is closed 
        session$onSessionEnded(function() { stopApp() }) 
    
    }) 
    

    如果ui.R改爲

    library(shiny) 
    shinyUI( fluidPage(fluidRow(
        column("2014-15", 
         style = 'padding:25px', 
         textInput("N_PYR", NULL, width = '192px'), 
         textInput("RATE_PYR", NULL, width = '192px'), 
         width = 3), 
        column("2015-16", 
          style = 'padding:25px', 
          textInput("N_CYR", NULL, width = '192px'), 
          textInput("RATE_CYR", NULL, width = '192px'), 
          width = 3) 
    
    ))) 
    

    這是所需產品:

    enter image description here

    目標:聲明一個字符串變量,其值將NOT從用戶端給出(這將在R代碼進行手動更改),以便每當CYR值被改變的UI被更新。這個變量是否在ui.R或server.R中聲明並不重要。

    回答

    1

    看起來像renderUI是你所需要的。另外請注意,您的substr是不正確的我固定它

    #rm(list = ls()) 
    library(shiny) 
    ui <- fluidPage(uiOutput("ui1")) 
    
    server <- function(input, output, session) { 
        # current year ("2015-16" for example) 
        CYR <- reactive({"2015-16"}) 
        PYR <- reactive({ 
        paste0(
         as.numeric(substr(CYR(), start = 1, stop = 4))-1, 
         "-", 
         as.numeric(substr(CYR(), start = 6, stop = 7))-1) 
        }) 
    
        output$ui1 <- renderUI({ 
        fluidRow(
        column(PYR(), 
          style = 'padding:25px', 
          textInput("N_PYR", NULL, width = '192px'), 
          textInput("RATE_PYR", NULL, width = '192px'), 
          width = 3), 
        column(CYR(), 
          style = 'padding:25px', 
          textInput("N_CYR", NULL, width = '192px'), 
          textInput("RATE_CYR", NULL, width = '192px'), 
          width = 3)) 
        }) 
        # terminate when window is closed 
        session$onSessionEnded(function() { stopApp() }) 
    } 
    
    shinyApp(ui, server) 
    

    enter image description here

    相關問題