2014-01-15 219 views
0

我有一個Shiny應用程序,我想在現有的網站中實現,因此我需要將ui.R轉換爲HTML UI。我得到了大部分的來自ui.R的東西在HTML UI中工作,但我有關於uiOutput,html輸出和文件上傳的問題。R Shiny:轉換成HTML UI uiOutput,htmlOutput和文件上傳

Q1:如何實現一個動態滑塊,我創建了renderUI({})?我用renderUI({})創建的動態選擇工作正常,但滑塊出現以下錯誤:min, max, amd value must all be numeric values似乎沒有數值可以從HMTL UI發送到server.R。

Q2::HMTL文件上傳工作將會如何進行?我似乎上傳文件,但我無法將它傳遞給server.R。

下面是一個例子:

server.R:

library(shiny) 

#sample data 
years<-c(1990,1995,2000,2005,2010) 
oryear<-years[3] 

shinyServer(function(input, output, session) { 

    #Input uploaded file 
    inFile<-input$ascii_layer 

    #make dynamic selection  
    output$selectUI <- renderUI({ 
    selectInput("test_select", "Test selection", years, selected=oryear) 
    }) 

    #make dynamic slider 
    output$slider <- renderUI({ 
    sliderInput("inSlider", "Slider", min=input$min_val, max=input$max_val, value=2000) 
    }) 

}) 

ui.R:

library(shiny) 

shinyUI(pageWithSidebar(
headerPanel("Test Shiny App"), 

    sidebarPanel(

    #File Upload 
     fileInput('ascii_layer', 'Choose ASCII Layer', multiple=FALSE, accept='asc'), 

    #HTML Selection Output from server.R  
    htmlOutput("selectUI"), 

    #Numeric Inputs 
     numericInput("min_val", "Enter Minimum Value", 1993), 
     numericInput("max_val", "Enter Maximum Value", 2013) 

    #display dynamic UI 
     uiOutput("slider") 
     ), 

    mainPanel() 
    )) 

HMTL UI:

<html> 

<head> 
    <script src="shared/jquery.js" type="text/javascript"></script> 
    <script src="shared/shiny.js" type="text/javascript"></script> 
    <link rel="stylesheet" type="text/css" href="shared/shiny.css"/> 
</head> 

<body> 
    <h1>HTML UI</h1> 

<!—- File Upload—-> 
    <p> 
    <form action="input_file.htm" method="post" enctype="multipart/form-data"> 
    <p>Choose Distance to Road Layer:<br> 
    <input name="ascii_layer" type="file" size="50" maxlength="100000" accept="*.asc"> 
    </p> 
    </form> 
    </p> 

<!—-Numeric Inputs—-> 
    <p> 
    <label>Enter Minimum Value:</label><br /> 
    <input type="number" name="min_val" value="1993" /> 
    </p> 

    <p> 
    <label>Enter Maximum Value:</label><br /> 
    <input type="number" name="max_val" value="1993" /> 
    </p> 


<!—-Dynamic Selection—-> 
    <div id="selectUI" class="shiny-html-output"></div> 

<!—-Dynamic Slider—-> 
    <div id="slider" class="shiny-html-output"></div> 


</body> 

</html> 

回答

2

你的Shiny代碼有幾個錯誤。下面給出了一個固定的版本。通過複製粘貼到R終端並通過瀏覽器查看源代碼來獲得您的html-ui。我建議你在網上看看Shiny教程(例如http://rstudio.github.io/shiny/tutorial)以獲取文件上傳等更多細節。

library(shiny) 

#sample data 
years <- c(1990,1995,2000,2005,2010) 
oryear <- years[3] 

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

    #Input uploaded file 
    # inFile<-input$ascii_layer 

    output$contents <- renderTable({ 
    inFile <- input$ascii_layer 

    if (is.null(inFile)) 
     return(NULL) 

    read.csv(inFile$datapath) 
    }) 

    #make dynamic selection  
    output$selectUI <- renderUI({ 
    selectInput("test_select", "Test selection", years, selected=oryear) 
    }) 

    #make dynamic slider 
    output$slider <- renderUI({ 
    sliderInput("inSlider", "Slider", min=input$min_val, max=input$max_val, value=2000) 
    }) 
} 

ui <- pageWithSidebar(

    headerPanel("Test Shiny App"), 
    sidebarPanel(

    #File Upload 
    fileInput('ascii_layer', 'Choose ASCII Layer', multiple=FALSE, accept='asc'), 

    #HTML Selection Output from server.R  
    uiOutput("selectUI"), 

    #Numeric Inputs 
    numericInput("min_val", "Enter Minimum Value", 1993), 
    numericInput("max_val", "Enter Maximum Value", 2013), 

    #display dynamic UI 
    uiOutput("slider") 

), 
    mainPanel(
    tableOutput('contents') 
) 
) 

runApp(list(ui = ui, server = server)) 
+0

謝謝@Vincent。檢查瀏覽器中的源代碼對我來說是個竅門。 –