【发布时间】:2021-02-03 04:43:12
【问题描述】:
我正在研究名为 Reticulate 的 R Shiny 包。
我在关注一个 Youtube,但出现错误。
R 闪亮代码
library(shiny)
library(reticulate)
source_python("practice2_simulator.py")
ui <- fluidPage(
titlePanel("Using SHiny with Python"),
sidebarLayout(
sidebarPanel(
numericInput(
'num_tosses',
label = "Number of Coin Tosses",
value = 50
),
numericInput(
'num_sims',
label = "Number of Simulations",
value = 1000
),
sliderInput(
'bias',
label = "Probability of Obstaining Heads",
value = 0.5,
min = 0,
max = 1,
step = 0.1
)
),
mainPanel(
plotOutput('heads_plot')
)
)
)
server <- function(input, output) {
output$heads_plot <- renderPlot({
sims <- head_dist(input$num_tosses,
input$num_sims,
input$bias)
hist(sims, col = "#75AADB", border = 'white', xlab = "Number of Heads")
})
}
shinyApp(ui, server)
Python 代码
from random import random
def head_dist(num_tosses, num_sims, bias):
return [num_heads(num_tosses, bias) for in x range(num_sims)]
def num_heads(num_tosses, bias):
return sum([random() < bias for x in range(num_tosses)])
我收到以下错误
py_run_file_impl(文件,本地,转换)中的错误:
SyntaxError:无效语法(第 4 行)
第 4 行是
source_python("practice2_simulator.py")
你能帮我解释一下为什么会出现这样的错误吗?
【问题讨论】:
标签: python r shiny reticulate