[英]how to integerate R script in Shiny web page?
在R腳本中,我通過提供培訓構建了一個svm模型。 在閃亮的網頁中,我設計了一個用戶界面,其中放置了一個文本框和兩個文本區域以從用戶那里獲得輸入。
現在我希望當用戶提交按鈕時,文本框和textarea值應該由R腳本程序使用,其中對模型進行訓練,現在使用新輸入通過內置模型對數據進行分類。
那么如何將閃亮網頁的文本字段值傳遞給R腳本?
ui <- fluidPage( fluidRow(id="form-row",column(6,offset =3 ,id="form-col",p(id="mandatory","'*' Fields are manadatory"),div(id = "form",
div(id="p-container",p(id="form-header","Your Question")),
div(id="form-field-container",
textInput("title", "*Title", width='100%',placeholder="Your Title is here..."),
# verbatimTextOutput("value"),
textOutput("title_val"),
#for text area width 100%
tags$style(HTML(".shiny-input-container:not(.shiny-input-container-inline){width: 100%;}")),
textAreaInput("description", "*Description", width='100%',rows = 3, resize = "both",placeholder="Description..."),
textOutput("desc_val"),
textAreaInput("code", "*Code", width='100%',rows = 3, resize = "both",placeholder="Write your code..."),
textOutput("code_val"),
useShinyalert(),
actionButton("submit", "Submit", class = "btn-primary")
))
)
)
)
server <- function(input, output, session){
observeEvent( input$submit,{
title_val <- as.character(input$title)
desc_val <- as.character(input$description)
code_val <- as.character(input$code)
ques<- paste(desc_val, code_val,sep=" \n")
shinyalert( title_val, ques, type = "success")
})
}
shinyApp(ui=ui, server=server)
你可以這樣做:
eval(parse(text = input$code))
您可以使用eval
和parse
來計算字符串。
library(shiny)
library(tidyverse)
library(shinyalert)
ui <- fluidPage( fluidRow(id="form-row",column(6,offset =3 ,id="form-col",p(id="mandatory","'*' Fields are manadatory"),div(id = "form",
div(id="p-container",p(id="form-header","Your Question")),
div(id="form-field-container",
textInput("title", "*Title", width='100%',placeholder="Your Title is here..."),
# verbatimTextOutput("value"),
textOutput("title_val"),
#for text area width 100%
tags$style(HTML(".shiny-input-container:not(.shiny-input-container-inline){width: 100%;}")),
textAreaInput("description", "*Description", width='100%',rows = 3, resize = "both",placeholder="Description..."),
textOutput("desc_val"),
textAreaInput("code", "*Code", width='100%',rows = 3, resize = "both",placeholder="Write your code..."),
textOutput("code_val"),
useShinyalert(),
actionButton("submit", "Submit", class = "btn-primary")
))
)
)
)
server <- function(input, output, session){
model <- reactive({
data <- mtcars # use your own data, might it be in a fileinput or some other data
result <- eval(parse(text = input$code)) # Should include a hint to what way your dataframe is called and how it looks.
result
})
observeEvent( input$submit,{
title_val <- as.character(input$title)
desc_val <- as.character(input$description)
code_val <- as.character(input$code)
ques<- paste(desc_val, code_val,sep=" \n")
shinyalert( title_val, ques, type = "success")
})
}
shinyApp(ui=ui, server=server)
但說實話,我不喜歡從用戶輸入中解析和評估文本的想法。 太多的可能性會遇到麻煩。
當我必須復制並粘貼腳本作為輸入時,不熟悉R的人的主要優點就消失了
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.